<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.application.component.model');

/**
 * JWPBlog default Model
 * 
 * wrapper for wp-include/functions.php, wp-admin/admin-functions.php and others
 * 
 *
 * @author	David Neimeyer <davidneimeyer@gmail.com>
 * @package		JWPBlog
 */

class JWPBlogModel extends JModel
{
	function __construct(){ //1.5
		parent::__construct();		
	}
	function getDB(){
		return JRegistry::getInstance('mojo')->getValue('mojo_db');
	}
	function get_users_drafts( $my_id ){
		return get_users_drafts( $my_id );
	}
	
	function get_others_drafts( $my_id ){
		return get_others_drafts( $my_id );
	}
	
	function wp(){
		return wp();
	}
	
	function get_results($sql){		
		$db = $this->getDB();
		$this->results = $db->get_results($sql);
		return $this->results;
	}
	
	function getData($arg){ //1.5
		return $this->getDB()->$arg;
	}
	
	function getPosts(){
		global $posts;
		$reg = JRegistry::getInstance('mojo');
		return $reg->getValue('wp.query.posts',$posts);
	}
	
	function apply_filters($filter, $data){
		return apply_filters($filter, $data);
	}
	
	/**
	 * Prepares wp query post entry
	 * replaces start_wp() and setup_postdata()
	 *
	 * @param object $post wp_query row object
	 */
	function preparePostData(&$post){ //1.5
		global $id, $postdata, $authordata, $day, $page, $pages, $multipage, $more, $numpages, $wp_query;
		global $pagenow;

		$id = $post->ID;

		$authordata = get_userdata($post->post_author);

		$day = mysql2date('d.m.y', $post->post_date);
		$currentmonth = mysql2date('m', $post->post_date);
		$numpages = 1;
		$page = get_query_var('page');
		if ( !$page )
			$page = 1;
		if ( is_single() || is_page() )
			$more = 1;
		$content = $post->post_content;
		if ( preg_match('/<!--nextpage-->/', $content) ) {
			if ( $page > 1 )
				$more = 1;
			$multipage = 1;
			$content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
			$content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
			$content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
			$pages = explode('<!--nextpage-->', $content);
			$numpages = count($pages);
		} else {
			$pages[0] = $post->post_content;
			$multipage = 0;
		}
		return true;
	}
	
	function preparePosts(&$posts){
		foreach($posts as $post){
			$post->date = $this->getPostDate($post);
			$post->id = $this->getPostID($post);
			$post->title = $this->getPostTitle($post);
			$post->categories = $this->getPostCategories($post);
			$post->author = $this->getPostAuthor($post);
			$post->permalink = $this->getPostPermalink($post);
		}
		return true;
	}
	/**
	 * deprecated; use preparePostData in JWPBlogModel
	 *
	 * @param object $post wp query row object
	 */
	function setup_postdata($post) {
		$this->preparePostData($post);
	}
	
	function nextPost(){
		$wp_query = $reg->getValue('wp.query.obj');
		return $wp_query->next_post();
	}
	
	function getPostDate($post,$gmt = false){
		$r = ($gmt)?$post->post_date:$post->post_date_gmt;
		return $r;
	}
	function getPostID($post){
		$id_field = 'ID'; //the mojo code is inconsistent, assumed to be related to wp changes; this is for compatibility
		return $post->$id_field;
	}
	function getPostTitle($post){
		$title_field = 'post_title';
		return $post->$title_field;
	}
	function getPostCategories($post){
		return false; //the_category(',')
	}
	function getPostAuthor($post){
		return false;//the_author()
	}
	function getPostPermaLink($post){
		return false; //the_permalink();
	}
function get_permalink($id = 0) {
	
	global $mosConfig_live_site, $database, $Itemid;
    
    $database->setQuery("SELECT id FROM #__menu WHERE link='index.php?option=com_mojo' AND published='1'");
    $Itemid = $database->loadResult();
    
	$rewritecode = array(
		'%year%',
		'%monthnum%',
		'%day%',
		'%hour%',
		'%minute%',
		'%second%',
		'%postname%',
		'%post_id%',
		'%category%',
		'%author%',
		'%pagename%'
	);
	
	$post = &get_post($id);
	if ( $post->post_status == 'static' )
		return get_page_link($post->ID);
	elseif ($post->post_status == 'attachment')
		return get_attachment_link($post->ID);

	$permalink = get_settings('permalink_structure');

	if ( '' != $permalink && 'draft' != $post->post_status ) {
		$unixtime = strtotime($post->post_date);

		$category = '';
		if ( strstr($permalink, '%category%') ) {
			$cats = get_the_category($post->ID);
			$category = $cats[0]->category_nicename;
			if ( $parent=$cats[0]->category_parent )
				$category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
		}

		$authordata = get_userdata($post->post_author);
		$author = $authordata->user_nicename;
		$rewritereplace = 
		array(
			date('Y', $unixtime),
			date('m', $unixtime),
			date('d', $unixtime),
			date('H', $unixtime),
			date('i', $unixtime),
			date('s', $unixtime),
			$post->post_name,
			$post->ID,
			$category,
			$author,
			$post->post_name,
		);
		return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
	} else { // if they're not using the fancy permalink option
		if ( function_exists('sefRelToAbs')) {
			$permalink = sefRelToAbs( 'index.php?option=com_mojo&amp;Itemid='.$Itemid.'&amp;p='.$post->ID );
		} else {
			$permalink = $mosConfig_live_site . '/index.php?option=com_mojo&amp;Itemid='.$Itemid.'&amp;p=' . $post->ID;
		}
		return apply_filters('post_link', $permalink, $post);
	}
}
}
?>